from fastapi import APIRouter, Depends, Request
from sqlalchemy.ext.asyncio import AsyncSession
from typing import Union

from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.common_vo import CrudResponseModel
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_kylin.entity.vo.future_account_daily_vo import (
    DeleteFutureAccountDailyModel,
    FutureAccountDailyModel,
    FutureAccountDailyPageQueryModel,
)
from module_kylin.service.future_account_daily_service import FutureAccountDailyService
from utils.log_util import logger
from utils.response_util import ResponseUtil

futureAccountDailyController = APIRouter(
    prefix='/kylin/futureAccountDaily', dependencies=[Depends(LoginService.get_current_user)]
)


@futureAccountDailyController.get(
    '/list', dependencies=[Depends(CheckUserInterfaceAuth('kylin:futureAccountDaily:list'))]
)
async def get_future_account_daily_list(
    request: Request,
    page_query_model: FutureAccountDailyPageQueryModel = Depends(FutureAccountDailyPageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
    is_page: bool = True,
):
    """
    获取期货账户资金日报列表
    """
    account_daily_list_result = await FutureAccountDailyService.get_account_daily_list(
        query_db, page_query_model, is_page
    )
    logger.info('获取期货账户资金日报列表成功')
    return ResponseUtil.success(model_content=account_daily_list_result)


@futureAccountDailyController.get(
    '/{daily_id}',
    dependencies=[Depends(CheckUserInterfaceAuth('kylin:futureAccountDaily:query'))],
)
async def query_detail_future_account_daily(
    request: Request,
    daily_id: int,
    query_db: AsyncSession = Depends(get_db),
):
    """
    获取期货账户资金日报详情
    """
    account_daily_detail_result = await FutureAccountDailyService.get_account_daily_detail(query_db, daily_id)
    logger.info(f'获取期货账户资金日报{daily_id}的信息成功')
    return ResponseUtil.success(data=account_daily_detail_result)


@futureAccountDailyController.post('', dependencies=[Depends(CheckUserInterfaceAuth('kylin:futureAccountDaily:add'))])
@Log(title='期货账户资金日报管理', business_type=BusinessType.INSERT)
async def add_future_account_daily(
    request: Request,
    account_daily: FutureAccountDailyModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    """
    新增期货账户资金日报
    """
    add_account_daily_result = await FutureAccountDailyService.add_account_daily(query_db, account_daily)
    logger.info(f'用户{current_user.user.user_name}新增期货账户资金日报成功')
    return ResponseUtil.success(data=add_account_daily_result)


@futureAccountDailyController.put(
    '/{daily_id}', dependencies=[Depends(CheckUserInterfaceAuth('kylin:futureAccountDaily:edit'))]
)
@Log(title='期货账户资金日报管理', business_type=BusinessType.UPDATE)
async def update_future_account_daily(
    request: Request,
    daily_id: int,
    account_daily: FutureAccountDailyModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    """
    更新期货账户资金日报
    """
    update_account_daily_result = await FutureAccountDailyService.update_account_daily(
        query_db, daily_id, account_daily
    )
    logger.info(f'用户{current_user.user.user_name}更新期货账户资金日报{daily_id}成功')
    return ResponseUtil.success(data=update_account_daily_result)


@futureAccountDailyController.delete(
    '', dependencies=[Depends(CheckUserInterfaceAuth('kylin:futureAccountDaily:remove'))]
)
@Log(title='期货账户资金日报管理', business_type=BusinessType.DELETE)
async def delete_future_account_daily(
    request: Request,
    delete_model: DeleteFutureAccountDailyModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    """
    删除期货账户资金日报
    """
    delete_account_daily_result = await FutureAccountDailyService.delete_account_daily(query_db, delete_model)
    logger.info(f'用户{current_user.user.user_name}删除期货账户资金日报成功')
    return ResponseUtil.success(msg=delete_account_daily_result.message)


@futureAccountDailyController.get(
    '/export', dependencies=[Depends(CheckUserInterfaceAuth('kylin:futureAccountDaily:export'))]
)
async def export_future_account_daily(
    request: Request,
    page_query_model: FutureAccountDailyPageQueryModel = Depends(),
    query_db: AsyncSession = Depends(get_db),
):
    """
    导出期货账户资金日报
    """
    try:
        export_result = await FutureAccountDailyService.export_account_daily(query_db, page_query_model)
        logger.info('导出期货账户资金日报成功')
        return export_result
    except Exception as e:
        logger.error(f'导出期货账户资金日报失败: {str(e)}')
        return ResponseUtil.error(msg=f'导出失败: {str(e)}')
